<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增blog')"/>
    <link rel="stylesheet" th:href="@{/editormd/css/editormd.min.css}">
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">标题：</label>
                <div class="layui-input-block">
                    <input type="text" name="title" placeholder="请输入标题"
                           class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否原创：</label>
                <div class="layui-input-block">
                    <select name="appreciation"  lay-verify="required" >
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">开启评论：</label>
                <div class="layui-input-block">
                    <select name="commentabled"  lay-verify="required" >
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">博客内容：</label>
                <div id="md-content" class="layui-input-block">
                    <textarea name="content"  placeholder="请输入博客内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">描述：</label>
                <div class="layui-input-block">
                    <input type="text" name="description" placeholder="请输入描述"
                        class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">显示图片</label>
                <div class="layui-upload">
                    <input type="hidden" name="firstPicture" />
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <div class="layui-upload-list" >
                        <img class="layui-upload-img" id="demo1" style=" margin-left:109px; width: 330px;height: 230px;">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否发布：</label>
                <div class="layui-input-block">
                    <select name="published"  lay-verify="required" >
                        <option value="">所有</option>
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="">
                <label class="layui-form-label">标签：</label>
                <div class="layui-input-block">
                    <select name="tag" xm-select="select" xm-select-skin="default">
                        <option th:each="tag : ${tags}" th:value="${tag.id}" th:text="${tag.name}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">置顶：</label>
                <div class="layui-input-block">
                   <select name="recommend"  lay-verify="required" >
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">允许分享：</label>
                <div class="layui-input-block">
                    <select name="shareStatement"  lay-verify="required" >
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">类型：</label>
                <div class="layui-input-block">
                    <select name="typeId"  lay-verify="required" >
                        <option th:each="type : ${types}" th:value="${type.id}" th:text="${type.name}"></option>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="blog-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
</body>
<th:block th:include="include :: footer"/>
<script th:src="@{/editormd/editormd.js}"></script>
<script th:inline="javascript">

    layui.use(['select','element','form', 'jquery', 'upload'], function () {
        let form = layui.form;
        let select = layui.select;
        let $ = layui.jquery;
        let upload = layui.upload;
        let prefix = "";


        form.on('submit(blog-save)', function (data) {
            for (var key in data.field) {
                var type = $(data.form).find("input[name='" + key + "']").attr("type");
                if (type == "checkbox") {
                    var value = "";
                    $(data.form).find("input[name='" + key + "']:checked").each(function () {
                        value += $(this).val() + ",";
                    })
                    if (value != "") {
                        value = value.substr(0, value.length - 1);
                        data.field[key] = value;
                    }

                }
            }
            $.ajax({
                url: prefix + 'save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.layui.table.reload("blog-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //初始化Markdown编辑器
        var contentEditor;
        $(function() {
            contentEditor = editormd("md-content", {
                width   : "auto",
                height  : 640,
                syncScrolling : "single",
                path    : pName + "/editormd/lib/"
            });
        });

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: pName + '/admin/files/uploadImg'
            ,field:"layuiFile"
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 1){
                    return layer.msg('上传失败');
                }
                //上传成功
                $("[name=firstPicture]").val(res.data.url);
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });

</script>
</html>